using System;
using System.Collections.Generic;
using UnityEngine;

public class CupsManager : MonoBehaviour
{
	private const int MAX_START_ROUND = 300;

	private static CupsManager instance;

	public static string keyCurrentCups = "keyCurrentCups";

	public static string keyCurrentLeague = "keyCurrentLeague";

	private static string keyLevelMovedToCups = "keyLevelMovedToCups";

	[SerializeField]
	private List<CupsInfo> cupsInfo;

	private static League startGameLeague;

	private static int sesionKillCount;

	private static int sesionDeadCount;

	private static int sesionCupsCount;

	private static League sesionLeagueChanged;

	private static float leaderTime;

	private static int maxStarsInCurrentRound;

	public static CupsManager Instance
	{
		get
		{
			if (instance == null)
			{
				UnityEngine.Object.Instantiate(Resources.Load<GameObject>("SessionReputationManager")).name = "SessionReputationManager(Don't Destroy)";
			}
			return instance;
		}
	}

	public List<CupsInfo> CupsInfo
	{
		get
		{
			return cupsInfo;
		}
	}

	public static int CurrentLeagueIndex
	{
		get
		{
			return BCWStorage.Get<int>(keyCurrentLeague);
		}
		set
		{
			BCWStorage.Set(keyCurrentLeague, value);
		}
	}

	public static int CurrentTier
	{
		get
		{
			return CurrentLeagueIndex + 1;
		}
	}

	public static League CurrentLeague
	{
		get
		{
			return Instance.cupsInfo[CurrentLeagueIndex].league;
		}
	}

	public static CupsInfo CurrentLeagueInfo
	{
		get
		{
			return Instance.cupsInfo[CurrentLeagueIndex];
		}
	}

	public static League StartGameLeague
	{
		get
		{
			return startGameLeague;
		}
	}

	public static int SesionKillCount
	{
		get
		{
			return sesionKillCount;
		}
	}

	public static int SesionDeadCount
	{
		get
		{
			return sesionDeadCount;
		}
	}

	public static int SesionCupsCount
	{
		get
		{
			return sesionCupsCount;
		}
	}

	public static League SesionLeagueChanged
	{
		get
		{
			return sesionLeagueChanged;
		}
	}

	public static float LeaderTime
	{
		get
		{
			return leaderTime;
		}
		set
		{
			if (GameController.instance.curTypeGame != TypeGame.Zombe && value > leaderTime)
			{
				Cups += (int)((value - leaderTime) * Instance.cupsInfo[CurrentLeagueIndex].cupsAddForsLeaderTime);
			}
			leaderTime = value;
		}
	}

	public static float GetLeagueFillAmount
	{
		get
		{
			int currentLeagueIndex = CurrentLeagueIndex;
			if (currentLeagueIndex < Instance.cupsInfo.Count - 1)
			{
				return (float)(Cups - Instance.cupsInfo[currentLeagueIndex].cupsCount) / (float)(Instance.cupsInfo[currentLeagueIndex + 1].cupsCount - Instance.cupsInfo[currentLeagueIndex].cupsCount);
			}
			return 1f;
		}
	}

	public static float GetSessionLeagueFillAmount
	{
		get
		{
			int currentLeagueIndex = CurrentLeagueIndex;
			if (currentLeagueIndex < Instance.cupsInfo.Count - 1)
			{
				return (float)sesionCupsCount / (float)(Instance.cupsInfo[currentLeagueIndex + 1].cupsCount - Instance.cupsInfo[currentLeagueIndex].cupsCount);
			}
			return 0f;
		}
	}

	public static int Cups
	{
		get
		{
			return BCWStorage.Get<int>(keyCurrentCups);
		}
		set
		{
			if (value < 0)
			{
				value = 0;
			}
			if (GetMaxStarsInCurrentRound() > 0)
			{
				value = Math.Min(value, GetMaxStarsInCurrentRound());
			}
			int num = value - Cups;
			sesionCupsCount += num;
			BCWStorage.Set(keyCurrentCups, value);
			if (CupsManager.cupsChanged != null)
			{
				CupsManager.cupsChanged(value);
			}
			int currentLeagueIndex = CurrentLeagueIndex;
			for (int i = currentLeagueIndex; i < Instance.cupsInfo.Count; i++)
			{
				if (i < Instance.cupsInfo.Count - 1 && value >= Instance.cupsInfo[i + 1].cupsCount)
				{
					sesionLeagueChanged = Instance.cupsInfo[i + 1].league;
					CurrentLeagueIndex = i + 1;
					if (CupsManager.leagueChanged != null)
					{
						CupsManager.leagueChanged(sesionLeagueChanged);
					}
				}
				else if (i > 0 && value <= Instance.cupsInfo[i].cupsCountToLoss)
				{
					sesionLeagueChanged = Instance.cupsInfo[i - 1].league;
					CurrentLeagueIndex = i - 1;
					if (CupsManager.leagueChanged != null)
					{
						CupsManager.leagueChanged(sesionLeagueChanged);
					}
					break;
				}
			}
		}
	}

	public static event Action<int> cupsChanged;

	public static event Action<League> leagueChanged;

	private void Awake()
	{
		instance = this;
		UnityEngine.Object.DontDestroyOnLoad(base.gameObject);
		if (!BCWStorage.Get<bool>(keyLevelMovedToCups))
		{
			int num = BCWStorage.Get<int>(settings.keyCurLevel);
			switch (num)
			{
			case 1:
				Cups = 0;
				break;
			case 2:
				Cups = 200;
				break;
			case 3:
				Cups = 300;
				break;
			case 4:
				Cups = 500;
				break;
			case 5:
				Cups = 1000;
				break;
			case 6:
			case 7:
			case 8:
			case 9:
			case 10:
			case 11:
			case 12:
			case 13:
			case 14:
			case 15:
			case 16:
			case 17:
			case 18:
			case 19:
			case 20:
			case 21:
			case 22:
			case 23:
			case 24:
			case 25:
				Cups = 2000 + (num - 6) * 200;
				break;
			}
			BCWStorage.Set(keyLevelMovedToCups, true);
		}
		startGameLeague = CurrentLeague;
	}

	public static League GetLeagueForIndex(int index)
	{
		if (index >= 0 && index < Instance.cupsInfo.Count)
		{
			return Instance.cupsInfo[index].league;
		}
		return League.none;
	}

	public static int GetMaxStarsInCurrentRound()
	{
		if (TDMController.isTeamGameType)
		{
			return maxStarsInCurrentRound;
		}
		return 0;
	}

	public static void SetMaxStarsInCurrentRound()
	{
		maxStarsInCurrentRound = Cups + 300;
	}

	public static void OnKillPlayer()
	{
		if (GameController.instance.curTypeGame != TypeGame.Zombe && GameController.instance.curTypeGame != TypeGame.CopsAndRobbers && GameController.instance.curTypeGame != TypeGame.WalkingRobots)
		{
			Cups += Instance.cupsInfo[CurrentLeagueIndex].addCupsOnKill;
		}
		if (GameController.instance.curTypeGame == TypeGame.CopsAndRobbers)
		{
			Cups++;
		}
		if (GameController.instance.curTypeGame == TypeGame.normal)
		{
			sesionKillCount++;
		}
		ZombieModePlayerStatistics.AddKill();
	}

	public static void OnPlayerDeath(bool online)
	{
		if (online && GameController.instance.curTypeGame != TypeGame.Zombe && GameController.instance.curTypeGame != TypeGame.CopsAndRobbers)
		{
			Cups -= Instance.cupsInfo[CurrentLeagueIndex].removeCupsOnDeath;
		}
		sesionDeadCount++;
	}

	public static void OnPlayerKilledHimself(bool online)
	{
		if (online && GameController.instance.curTypeGame != TypeGame.Zombe && GameController.instance.curTypeGame != TypeGame.CopsAndRobbers)
		{
			Cups -= Instance.cupsInfo[CurrentLeagueIndex].removeCupsOnDeath;
		}
		sesionDeadCount++;
	}

	public static void StartGame()
	{
		startGameLeague = CurrentLeague;
		sesionCupsCount = 0;
		sesionDeadCount = 0;
		sesionKillCount = 0;
		sesionLeagueChanged = League.none;
		SessionReputationManager.Instance.StartGame(startGameLeague);
	}

	public static void LoadLobby()
	{
	}

	public static List<string> GetListLeagueName()
	{
		List<string> listLeagueName = new List<string>();
		Instance.cupsInfo.ForEach(delegate(CupsInfo x)
		{
			listLeagueName.Add(x.league.ToString());
		});
		return listLeagueName;
	}

	public static void RestoreCups(int value)
	{
		BCWStorage.Set(keyCurrentCups, value, true);
		int num = 0;
		for (int i = 0; i < Instance.cupsInfo.Count && value > Instance.cupsInfo[i].cupsCount; i++)
		{
			num++;
		}
		CurrentLeagueIndex = Mathf.Clamp(num, 0, Instance.cupsInfo.Count - 1);
		if (CupsManager.leagueChanged != null)
		{
			CupsManager.leagueChanged(Instance.cupsInfo[CurrentLeagueIndex].league);
		}
		if (CupsManager.cupsChanged != null)
		{
			CupsManager.cupsChanged(value);
		}
		SessionReputationManager.Instance.DontGetReward();
		StartGame();
	}
}
